package com.haiqi.modules.map.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.ReportLineInfo;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.map.service.ReportLineInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HaiQi on 2017-2-27.
 */
@Controller
@RequestMapping("/manage/reportline/info")
@SafeAuthority(menuCode = "monitorLocation")
public class ReportLineInfoController extends BaseController {
    @Autowired
    public ReportLineInfoService reportLineInfoService;


    @RequestMapping("")
    public String reportline(){
        return "modules/map/window/reportLineInfo";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map insertReportLineInfo(ReportLineInfo reportLineInfo, HttpSession session){
        Map<String,Boolean> response = new HashMap<>();
        ResponseBean.UserBean user = getUser(session);
        if(org.apache.commons.lang.StringUtils.isNotEmpty(user.getName())){
            reportLineInfo.setAdduser(user.getName());
        }else{
            reportLineInfo.setAdduser(user.getLoginname());
        }//LINESTRING
        if (reportLineInfo.getLongitude()!=null&&reportLineInfo.getLatitude()!=null){
            if (reportLineInfo.getLongitude().length==reportLineInfo.getLatitude().length){
                String temp="";
                for (int i=0;i<reportLineInfo.getLongitude().length;i++){
                    temp+=reportLineInfo.getLongitude()[i]+" "+reportLineInfo.getLatitude()[i]+",";
                }

                temp="("+temp.substring(0,temp.length()-1)+")";
                reportLineInfo.setCoordinate("LINESTRING"+temp+"");
            }
        }
        boolean success = reportLineInfoService.save(reportLineInfo);
        response.put("success",success);
        return response;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map deleteReportLineInfo(String id){
        Map<String,Boolean> response = new HashMap<>();
        boolean success = reportLineInfoService.delete(id);
        response.put("success",success);
        return response;
    }
    @RequestMapping("/update/state")
    @ResponseBody
    public Map updateReportLineInfo(String state,String id){
        Map<String,Boolean> response = new HashMap<>();
        if(state.equals("1")){
            state = "2";
        }else if(state.equals("2")){
            state = "1";
        }else{
            state = "1";
        }
        boolean success = reportLineInfoService.updateStateById(state,id);
        response.put("success",success);
        return response;
    }

    @ResponseBody
    @RequestMapping("/ajax/list")
    public DataGridBean<ReportLineInfo> ajaxList(String type,PageBean pageBean){
        PageUtils.generatePage(pageBean);
        List<ReportLineInfo> list = null;

        if (StringUtils.isEmpty(type)){
            list = reportLineInfoService.findAll();
        }else if(type.equals("0")){
            list = reportLineInfoService.findAll();
        }else{
            list = reportLineInfoService.findAllByType(type);
        }

        PageInfo<ReportLineInfo> pageInfo = new PageInfo<>(list);
        DataGridBean<ReportLineInfo> gridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo,gridBean);
        return gridBean;
    }

    @RequestMapping("/one")
    @ResponseBody
    public ReportLineInfo getOneById(String id){
        return reportLineInfoService.getOneReportLineInfo(id);
    }
}
